using System;
using System.Collections.Generic;
using System.Net;
using System.Text;

// Project Server 2007 References
using Microsoft.Office.Project.Server.Library;

namespace Microsoft.EPM.Events
{
    /// <summary>
    /// Timesheet Helper Methods
    /// </summary>
    class TimesheetPSI
    {
        #region Properties

        private string  name;
        private string  resName;
        private string  creatorResName;
        private Guid    creatorResUID;
        private Guid    resUID;
        private Guid    periodUID;
        private decimal totalActValue;
        /// <summary>
        /// Timesheet Name
        /// </summary>
        public string Name
        {
            get { return name; }
        }
        /// <summary>
        /// Resource Name
        /// </summary>
        public string ResName
        {
            get { return resName; }
        }
        /// <summary>
        /// Timesheet creator resource name
        /// </summary>
        public string CreatorResName
        {
            get { return creatorResName; }
        }
        /// <summary>
        /// Timesheet creator resource UID 
        /// </summary>
        public Guid CreatorResUID
        {
            get { return creatorResUID; }
        }
        /// <summary>
        /// Resource UID
        /// </summary>
        public Guid ResUID
        {
            get { return resUID; }
        }
        /// <summary>
        /// Period UID
        /// </summary>
        public Guid PeriodUID
        {
            get { return periodUID; }
        }
        /// <summary>
        /// Total actual value in hours
        /// </summary>
        public decimal TotalActValue
        {
            get { return totalActValue; }
        }
        #endregion

        #region Constructor

        /// <summary>
        /// Timesheet header information
        /// </summary>
        /// <param name="timesheetDS">Timesheet DataSet</param>
        public TimesheetPSI(TimesheetWS.TimesheetDataSet timesheetDS)
        {
            name            = timesheetDS.Headers[0].TS_NAME;
            resName         = timesheetDS.Headers[0].TS_CACHED_RES_NAME;
            creatorResName  = timesheetDS.Headers[0].TS_CACHED_CREATOR_RES_NAME;
            creatorResUID   = timesheetDS.Headers[0].TS_CREATOR_RES_UID != Guid.Empty ? timesheetDS.Headers[0].TS_CREATOR_RES_UID : Guid.NewGuid();
            resUID          = timesheetDS.Headers[0].RES_UID != Guid.Empty ? timesheetDS.Headers[0].RES_UID : Guid.NewGuid();
            periodUID       = timesheetDS.Headers[0].WPRD_UID != Guid.Empty ? timesheetDS.Headers[0].WPRD_UID : Guid.NewGuid();
            totalActValue   = timesheetDS.Headers[0].TS_TOTAL_ACT_VALUE/60000;
        }

        #endregion
    }

    /// <summary>
    /// Perform WS impersonation
    /// </summary>
    class TimesheetDerived : TimesheetWS.TimeSheet
    {
        private String contextString = String.Empty;

        protected override WebRequest GetWebRequest(Uri uri)
        {
            WebRequest webRequest = base.GetWebRequest(uri);
            if (contextString != String.Empty)
            {
                webRequest.UseDefaultCredentials = true;
                webRequest.Credentials = CredentialCache.DefaultCredentials;
                webRequest.Headers.Add("PjAuth", contextString);
                webRequest.Headers.Add("ForwardFrom", "/_vti_bin/psi/timesheet.asmx");
                webRequest.PreAuthenticate = true;
            }
            return webRequest;
        }

        public void SetImpersonationContext(bool isWindowsUser, String userNTAccount, Guid userGuid, Guid trackingGuid, Guid siteId, String lcid)
        {
            contextString = GetImpersonationContext(isWindowsUser, userNTAccount, userGuid, trackingGuid, siteId, lcid);
        }

        private String GetImpersonationContext(bool isWindowsUser, String userNTAccount, Guid userGuid, Guid trackingGuid, Guid siteId, String lcid)
        {
            PSContextInfo contextInfo = new PSContextInfo(isWindowsUser, userNTAccount, userGuid, trackingGuid, siteId, lcid);
            return PSContextInfo.SerializeToString(contextInfo);
        }
    }
}
